#!/bin/bash - 
#===============================================================================
#
#          FILE:  get_scripts.sh
# 
#         USAGE:  ./get_scripts.sh 
# 
#   DESCRIPTION:  Script to get the required vim script from the http
#                   server vim.org
# 
#       OPTIONS:  ---
#  REQUIREMENTS:  ---
#          BUGS:  ---
#         NOTES:  ---
#        AUTHOR:  Amit Agarwal (AKA), amit.agarwal@amit-agarwal.co.in
#       COMPANY:  Individual
#       VERSION:  1.1
#       CREATED:  01/06/2010 10:06:27 PM IST
#      REVISION: Thu Mar 17 22:30:15 IST 2011
#===============================================================================


#-------------------------------------------------------------------------------
#   You mostly would not need to change the below params also,
#   but just in case you do not like to see my name or do 
#   not like the location for these files, then you can change the name of
#   files in the below variables.
#
#   As a side note, it is good idea to keep them in location where they are
#   not deleted automatically so that the same can be used over and over
#   again, without downloading them.
#-------------------------------------------------------------------------------

tmp_file="/tmp/vim_scripts.html"
script_id_file="/tmp/script_id"



##========Do not change below this line...======

#===  FUNCTION  ================================================================
#          NAME:  download_list
#   DESCRIPTION:  Download the list of vim scripts and put it in a file.
#    PARAMETERS:  NA
#       RETURNS:  NA
#===============================================================================
download_list()
{
    downloaded=1
    if [ ! -f $tmp_file ]
    then
        wget -O $tmp_file 'http://www.vim.org/scripts/script_search_results.php?&show_me=4000&result_ptr=0' -o /dev/null
    else
        echo "using the existing $tmp_file"
    fi
}

#===  FUNCTION  ================================================================
#          NAME:  search_list
#   DESCRIPTION:  Get the list of scripts from the vim.sf.net
#    PARAMETERS:  None
#       RETURNS:  Nothing
#===============================================================================
search_list ()
{
    
    read -p "Enter the text to search :: " text
    pager_exist=$(which $PAGER 2>/dev/null)
    if [ "$pager_exist" = "" ]
    then
        echo "No pager defined that can be used, displaying the first 10 entries"
        cat $tmp_file | grep width| grep script_id= |sed 's/.*script_id=//'|tr -d "\""|tr ">" " "|sed 's/<.*//' | tr -d '\001'-'\011''\013''\014''\016'-'\037''\200'-'\377' |grep -i "$text"
    else
        if [ "x$1" = "x" ]
        then
             cat $tmp_file | grep width| grep script_id= |sed 's/.*script_id=//'|tr -d "\""|tr ">" " "|sed 's/<.*//' | tr -d '\001'-'\011''\013''\014''\016'-'\037''\200'-'\377' |grep -i "$text"
        else
             cat $tmp_file | grep width| grep script_id= |sed 's/.*script_id=//'|tr -d "\""|tr ">" " "|sed 's/<.*//' | tr -d '\001'-'\011''\013''\014''\016'-'\037''\200'-'\377' |grep -i "$text"|$1
        fi
    fi
    echo "--$1--"

}
get_all_list()
{
    echo "Printing the list of the scripts with the script ID's"
    pager_exist=$(which $PAGER 2>/dev/null)
    if [ "$pager_exist" = "" ]
    then
        echo "No pager defined that can be used, displaying the first 10 entries"
        cat $tmp_file | grep width| grep script_id= |sed 's/.*script_id=//'|tr -d "\""|tr ">" " "|sed 's/<.*//' | tr -d '\001'-'\011''\013''\014''\016'-'\037''\200'-'\377' |sort -n|head -n 10
    else
        if [ "x$1" = "x" ]
        then
             cat $tmp_file | grep width| grep script_id= |sed 's/.*script_id=//'|tr -d "\""|tr ">" " "|sed 's/<.*//' | tr -d '\001'-'\011''\013''\014''\016'-'\037''\200'-'\377' |sort -n|$PAGER
        else
             cat $tmp_file | grep width| grep script_id= |sed 's/.*script_id=//'|tr -d "\""|tr ">" " "|sed 's/<.*//' | tr -d '\001'-'\011''\013''\014''\016'-'\037''\200'-'\377' |$PAGER
        fi
    fi
    echo "--$1--"

}

#===  FUNCTION  ================================================================
#          NAME:  get_details
#   DESCRIPTION:  Get the details of the Sript ID
#    PARAMETERS:  NA
#       RETURNS:  NA
#===============================================================================
get_details()
{
    echo -n "Enter the script ID to get the details :"
    read script_id
    if [ ! -f $script_id_file$script_id ] 
    then
        wget -o /dev/null -O $script_id_file$script_id 'http://www.vim.org/scripts/script.php?script_id='$script_id |html2text
    fi
    printf "%20s"
    printf "%20s\n\n\n" "+"|tr ' ' '+'

    h2t=`which html2text 2>/dev/null`
    if [ $h2t = "" ]
    then
        cat $script_id_file$script_id|sed --silent '/description/,/\/table/ p'
    else
        cat $script_id_file$script_id|sed --silent '/description/,/\/table/ p'|html2text
    fi
    printf "%20s"
    printf "%20s\n\n\n" "+"|tr ' ' '+'
}

#===  FUNCTION  ================================================================
#          NAME:  print_header
#   DESCRIPTION:  Print the header for the script
#    PARAMETERS:  NA
#       RETURNS:  NA
#===============================================================================
print_header()
{
    printf "\n\n\n%80s\n" "="|tr ' ' '='
    echo "        Welcome to VIM Scripts utility"
    echo "        Script written by Amit Agarwal"
    echo "        http://blog.amit-agarwal.co.in"
    printf "%80s\n" "="|tr ' ' '='

    cat << EOF
        Menu
        ====
        1. Get the list of all scripts
        2. Get the description for the script
        3. Download the script
        4. Add the script to GetVIMLatestScript
        5. Get selective list for scripts
        6. Delete the existing list of all scripts
        7. Get the list of all scripts in reverse order
        8. Search the plugin list.
        p. Change Pager
        q. Quit the script
        ----------------------------------------------
EOF
}

#===  FUNCTION  ================================================================
#          NAME:  get_script
#   DESCRIPTION:  Download the vim script
#    PARAMETERS:  NA
#       RETURNS:  NA
#===============================================================================
get_script()
{
    read -p "Enter the script ID to download :: " script_id
    echo "Downloading the description"
    if [ ! -f $script_id_file$script_id ]
    then
        wget -o /dev/null -O $script_id_file$script_id 'http://www.vim.org/scripts/script.php?script_id='$script_id 
    fi
    src_id_tmp=$(cat $script_id_file$script_id|grep src_id|head -1|sed -e 's/.*href="//')
    src_id=$(echo $src_id_tmp|sed  -e 's/".*//')
    filename=$(echo $src_id_tmp|sed  's/.*">//' |sed 's#<.*##')
    echo "Downloading the file as $filename"
    curl http://www.vim.org/scripts/$src_id 2>/dev/null > $filename
}


#===  FUNCTION  ================================================================
#          NAME:  add_script2GVLS
#   DESCRIPTION:  Add the script id to Get Latest VIM Scripts
#    PARAMETERS:  NA
#       RETURNS:  NA
#===============================================================================
add_script2GVLS()
{
    read -p "Enter the Script ID to add : " script_id 
    script_name=$(cat $tmp_file | grep width| grep script_id= |sed 's/.*script_id=//'|tr -d "\""|tr ">" " "|sed 's/<.*//' |sort -n|grep "^$script_id "|sed 's/\([0-9]*\)\(.*\)/\2/')
    echo "$script_id 1 $script_name"
    cp ~/.vim/GetLatest/GetLatestVimScripts.dat ~/.vim/GetLatest/GetLatestVimScripts.backup
    echo "$script_id 1 :AutoInstall: $script_name" >> ~/.vim/GetLatest/GetLatestVimScripts.dat
}


#===  FUNCTION  ================================================================
#          NAME:  get_list
#   DESCRIPTION:  Get the list of scripts for selective items
#    PARAMETERS:  NA
#       RETURNS:  NA
#===============================================================================
get_list()
{
    echo "Getting the web page for the list of scripts..."
    echo "Please be patient"
    
    read -p "Enter the script ID to start from :" start_id
    read -p "Enter the number of entries to display :" number_display
    echo "Printing the list of the scripts with the script ID's from $start_id"

    printf "\n\n%75s\n"|tr ' ' '='
    
    cat $tmp_file | grep width| grep script_id= |sed 's/.*script_id=//'|tr -d "\""|tr ">" " "|sed 's/<.*//' |sort -n|sed -n '/^'$start_id' /,$ p'|head -n $number_display 
    printf "%75s\n\n\n\n"|tr ' ' '='
}

#-------------------------------------------------------------------------------
#   This is where we start the actual script in a infinite
#   loop. You can exit the loop with q.
#-------------------------------------------------------------------------------
echo "Getting the list of scripts ...."
echo "Hold on a few seconds, while I do some dirty work :)"
download_list
while true
do


    print_header

    read -p "Please enter your choice : " read_choice

    case $read_choice in
        '1')
            get_all_list
            ;;
        '2')
            get_details
            ;;
        '3')
            get_script
            ;;
        '4')
            add_script2GVLS
            ;;
        '5')
            get_list
            ;;
        '6')
            rm -f $tmp_file
            ;;
        '7')
            get_all_list 1
            ;;
        '8')
            search_list
            ;;
        'p')
            echo "Current pager is $PAGER"
            read -p "Enter the name of the pager to use :: " pager
            export PAGER=$pager
            ;;
        'q')
            exit 0
            ;;
        *)
            echo "Not a valid input"
            ;;
    esac
done
